package org.mybatis.study.mapper;

import org.apache.ibatis.annotations.*;
import org.mybatis.study.model.SysRole;

import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/9/20 11:27 下午
 */
public interface RoleMapper {

    /**
     * 通过角色id 查询角色信息
     *
     * @param id 主键
     * @return SysRole
     */
    @Select({"select id,role_name, enabled, create_by, create_time from sys_role where id = #{id}"})
    SysRole selectById(Long id);

    /**
     * 通过角色id 查询角色信息
     * 使用id属性共享当前的Results结果集
     *
     * @param id 主键
     * @return SysRole
     */
    @Results(id = "roleResultMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "roleName", column = "role_name"),
            @Result(property = "enabled", column = "enabled"),
            @Result(property = "createBy", column = "create_by"),
            @Result(property = "createTime", column = "create_time")
    })
    @Select({"select id,role_name, enabled, create_by, create_time from sys_role where id = #{id}"})
    SysRole selectById2(Long id);

    /**
     * 查询所有角色信息 (使用ResultMap引入上面 id为roleResultMap 的Results结果集)
     *
     * @return List
     */
    @ResultMap("roleResultMap")
    @Select("select * from sys_role")
    List<SysRole> selectAll();

    /**
     * 新增角色 - 不需要返回主键
     *
     * @param sysRole 角色
     * @return int
     */
    @Insert({"insert into sys_role(id, role_name, enabled, create_by, create_time) values (#{id}, #{roleName}, #{enabled}, #{createBy}, #{createTime, jdbcType=TIMESTAMP})"})
    int insert(SysRole sysRole);

    /**
     * 新增角色 - 返回自增主键
     *
     * @param sysRole 角色
     * @return int
     */
    @Insert({"insert into sys_role(role_name, enabled, create_by, create_time) values (#{roleName}, #{enabled}, #{createBy}, #{createTime, jdbcType=TIMESTAMP})"})
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert2(SysRole sysRole);

    /**
     * 新增角色 - 返回非自增主键
     * resultType = Long.class 同 resultType="long"
     * before = false 同 XML order="BEFORE"
     *
     * @param sysRole 角色
     * @return int
     */
    @Insert({"insert into sys_role(role_name, enabled, create_by, create_time) values (#{roleName}, #{enabled}, #{createBy}, #{createTime, jdbcType=TIMESTAMP})"})
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", resultType = Long.class, before = false)
    int insert3(SysRole sysRole);

    /**
     * 用户更新 - @Update注解
     *
     * @param sysRole 用户
     * @return int
     */
    @Update({"update sys_role set role_name=#{roleName}, enabled=#{enabled}, create_by=#{createBy}, create_time=#{createTime, jdbcType=TIMESTAMP} where id=#{id} "})
    int updateById(SysRole sysRole);

    /**
     * 通过用户id删除用户 - @Delete注解
     *
     * @param id 用户id
     * @return int
     */
    @Delete("delete from sys_role where id=#{id}")
    int deleteById(Long id);


    /**
     * MyBatis 高级查询 => 一对多映射 -> collection 集合的嵌套结果映射 - 获取所有角色和对应的权限信息
     *
     * @return List<SysRole>
     */
    List<SysRole> selectAllRoleAndPrivileges();
}
